package com.api.controller;


import com.api.annotation.Login;
import com.api.common.utils.ConvertUtils;
import com.api.common.utils.R;
import com.api.common.utils.Result;
import com.api.entity.Item;
import com.api.entity.TbCarousel;
import com.api.entity.dto.ItemDTO;
import com.api.service.IFeedbackService;
import com.api.service.ITbCarouselService;
import com.api.service.UploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 轮播图 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-09-27
 */
@RestController
@RequestMapping("/api/carousel")
@AllArgsConstructor
public class TbCarouselController {

    private  final ITbCarouselService carouselService;

    @Autowired
    private UploadService uploadService;

    /**
     * 新增
     */
    @Login
    @PostMapping
    @Operation(summary = "新增")
    public Result save(@RequestBody TbCarousel carousel) {
        carousel.setPath(uploadService.deleteFile(carousel.getPath()));
        carouselService.save(carousel);
        return new Result();
    }
    /**
     * 列表
     */
    @Login
    @GetMapping
    @Operation(summary = "列表")
    public Result save() {

        LambdaQueryWrapper<TbCarousel> lwq = new LambdaQueryWrapper<>();
        lwq.orderByDesc(TbCarousel::getSort);
        List<TbCarousel> list = carouselService.list();
        ArrayList<TbCarousel> res = new ArrayList<>();
        for (TbCarousel carousel : list) {
            carousel.setPath(uploadService.toAbsoluteUrl(carousel.getPath()));
            res.add(carousel);
        }
        return new Result().ok(res);
    }
    /**
     * 修改
     */
    @Login
    @PutMapping
    @Operation(summary = "修改")
    public Result update(@RequestBody TbCarousel carousel) {
        carousel.setPath(uploadService.deleteFile(carousel.getPath()));
        carouselService.updateById(carousel);
        return new Result();
    }

    /**
     * 删除
     */
    @Login
    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    public Result del(@PathVariable Integer id) {
        System.out.println("删除编号" + id);
        carouselService.removeById(id);
        return new Result();
    }

}
